﻿using System;
using System.Globalization;

namespace mal.WP7.Framework.Helpers
{
    public class DateTimeHelper
    {
        /// <summary>
        /// Returns the first day of the week that the specified
        /// date is in using the current culture. 
        /// </summary>
        public static DateTime GetFirstDayOfWeek(DateTime date)
        {
            CultureInfo defaultCultureInfo = CultureInfo.CurrentCulture;
            return DateTimeHelper.GetFirstDayOfWeek(date, defaultCultureInfo);
        }

        /// <summary>
        /// Returns the first day of the week that the specified date is in. 
        /// </summary>
        public static DateTime GetFirstDayOfWeek(DateTime date, CultureInfo cultureInfo)
        {
            DayOfWeek firstDay = cultureInfo.DateTimeFormat.FirstDayOfWeek;
            DateTime firstDayInWeek = date.Date;
            while (firstDayInWeek.DayOfWeek != firstDay)
            {
                firstDayInWeek = firstDayInWeek.AddDays(-1);
            }

            return firstDayInWeek;
        }

        /// <summary>
        /// Returns the first day of the month that the specified
        /// date is in. 
        /// </summary>
        public static DateTime GetFirstDayOfMonth(DateTime date)
        {
            return new DateTime(date.Year, date.Month, 1);
        }

        /// <summary>
        /// Returns the first day of the year that the specified
        /// date is in. 
        /// </summary>
        public static DateTime GetFirstDayOfYear(DateTime date)
        {
            return new DateTime(date.Year, 1, 1);
        }
    }
}
